package catchData.core;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.lang.StringUtils;

import util.CatchDataUtil;
import util.Conts;

public class Item4CountryMedicine extends Thread {
	private static HttpClient client;
	private static final Logger LOG = Logger.getLogger("Item4CountryMedicine");
	private static final String LOGIN_PAGE = "http://app1.sfda.gov.cn/datasearch/face3/base.jsp?tableId=1&tableName=TABLE1&title=OTC%BB%AF%D1%A7%D2%A9%C6%B7%CB%B5%C3%F7%CA%E9%B7%B6%B1%BE&bcId=118622086116566219450310733363";
	private static final String HOST = "app1.sfda.gov.cn";
	
	private String path;
	private String fileName;
	private int start;
	private int end;
	private int tableId;
	private String bcId;
	private int seq;
	
	public Item4CountryMedicine() {
		super();
	}
	
	public Item4CountryMedicine(String path, String fileName, int start, int end, int tableId, String bcId, int seq) {
		this.path = path;
		this.fileName = fileName;
		this.start = start;
		this.end = end;
		this.tableId = tableId;
		this.bcId = bcId;
		this.seq = seq;
	}
	
	public String getPath() {
		return path;
	}
	public void setPath(String path) {
		this.path = path;
	}
	public String getFileName() {
		return fileName;
	}
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	public int getStart() {
		return start;
	}
	public void setStart(int start) {
		this.start = start;
	}
	public int getEnd() {
		return end;
	}
	public void setEnd(int end) {
		this.end = end;
	}
	public int getTableId() {
		return tableId;
	}
	public void setTableId(int tableId) {
		this.tableId = tableId;
	}
	public String getBcId() {
		return bcId;
	}
	public void setBcId(String bcId) {
		this.bcId = bcId;
	}
	public int getSeq() {
		return seq;
	}
	public void setSeq(int seq) {
		this.seq = seq;
	}
	
	protected void initHttpClient() {
		if (client == null) {
			MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
			client = new HttpClient(connectionManager);
			
			client.getParams().setCookiePolicy(
					CookiePolicy.BROWSER_COMPATIBILITY);
			client.getParams().setParameter(
					"http.protocol.single-cookie-header", true);
			client.getParams().setParameter(
					HttpMethodParams.HTTP_CONTENT_CHARSET, "gb2312");
			client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
			client.getHttpConnectionManager().getParams().setConnectionTimeout(300000);
//			client.getHttpConnectionManager().getParams().setSoTimeout(300000);
		}
	}
	
	protected void setHeaders(HttpMethod method) {
		method.getParams().setParameter("http.protocol.cookie-policy", CookiePolicy.BROWSER_COMPATIBILITY);
		method.setRequestHeader("accept","image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
		method.setRequestHeader("referer", LOGIN_PAGE);
		method.setRequestHeader("accept-encoding", "UTF-8,GBK,GB2312,gzip,deflate");
		method.setRequestHeader("accept-language", "zh-CN");
		method.setRequestHeader("content-type", "application/x-www-form-urlencoded");
		method.setRequestHeader("user-agent","Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; staticlogin:product=cboxf2010&act=login&info=ZmlsZW5hbWU9UG93ZXJXb3JkMjAxME94Zl9VbHRpbWF0ZS5leGUmbWFjPUY4RDgxQUY2MTgwQjQ3REQ5MUI5QkM4NjU3MzAwRkU3JnBhc3Nwb3J0PSZ2ZXJzaW9uPTIwMTAuNi4zLjYuMiZjcmFzaHR5cGU9MQ==&verify=b226e2d473bb8dbbb37568db14fee0e4; staticlogin:product=cboxf2010&act=login&info=ZmlsZW5hbWU9UG93ZXJXb3JkMjAxME94Zl9TcGVjaWFsLmV4ZSZtYWM9RjhEODFBRjYxODBCNDdERDkxQjlCQzg2NTczMDBGRTcmcGFzc3BvcnQ9JnZlcnNpb249MjAxMC42LjMuNS4yJmNyYXNodHlwZT0x&verify=94c65611a34cfb4964638346b072d76e; staticlogin:product=cboxf09&act=login&info=ZmlsZW5hbWU9UG93ZXJ3b3JkMjAwOU94Zi4yNTI2OS40MDExLmV4ZSZtYWM9RjhEODFBRjYxODBCNDdERDkxQjlCQzg2NTczMDBGRTcmcGFzc3BvcnQ9JnZlcnNpb249MjAwOS4wNS4yNS4zLjI3MiZjcmFzaHR5cGU9MQ==&verify=32e210faa8ce04f90dfc4225f98e80ac; aff-kingsoft-ciba; TheWorld)");
		method.setRequestHeader("host", HOST);
		method.setRequestHeader("connection", "Keep-Alive");
		method.setRequestHeader("cache-control", "no-cache");
	}
	
	protected void saveDataFile(String sFolder, String fileName, String data, int isidd) {
		File folder = new File(sFolder+"/"+fileName);
		if (!folder.exists()) folder.mkdirs();
		PrintWriter out = null;
		String saveFile = folder.toString() + "/" + isidd + ".htm";
		try {
			out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(saveFile), "gbk"));
			out.println(data);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (out != null) {
					out.close();
					out = null;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	public void run() {
		PostMethod method = null;
		PostMethod post = null;
		int endCount = 0;
		int endSeq = 0;
		try {
			String pathURL = "http://app1.sfda.gov.cn/datasearch/face3/search.jsp?tableId="+tableId+"&keyword=&bcId="+bcId+"&tableName=TABLE"+tableId+"&viewtitleName=COLUMN167&viewsubTitleName=COLUMN1662,COLUMN1702,COLUMN821&curstart=";
			String pathURL2 = "http://app1.sfda.gov.cn/datasearch/face3/content.jsp?tableView=&tableId="+tableId+"&tableName=TABLE"+tableId+"&Id=";
			String str = "";
			String regex="&Id=([^']*)";
			Pattern pattern = Pattern.compile(regex);
			Matcher matcher = null;
			String answer = "";
			initHttpClient();
			for(int i = start; i<= end; i++){
				method = new PostMethod(pathURL+i);
				setHeaders(method);
				int statueCode = client.executeMethod(method);
//				if(statueCode != HttpStatus.SC_OK) continue;
				str = method.getResponseBodyAsString();
				matcher = pattern.matcher(str);
				while (matcher.find()){
					answer += matcher.group().replaceAll("&Id=", "&").toString();
				}
				String[] ids = answer.split("&");
				answer = "";
				for(int j = 1; j < ids.length; j++){
					post = new PostMethod(pathURL2+ids[j]);
					setHeaders(post);
					client.executeMethod(post);
					str = post.getResponseBodyAsString();
					saveDataFile(path,fileName, str+"\n id is 【" + ids[j] + "】\n", ++seq);
					post.releaseConnection();
				}
				method.releaseConnection();
//				LOG.info("Current file is: " + fileName +" and has "+ seq +" rows;Next page is NO. "+(i+1));
				endCount = i;
				endSeq = seq;
			}
		} catch (Exception e) {
				LOG.info(fileName + " did not go on, next page " + (endCount+1) + " and seq " + endSeq);
		} finally {
			post.releaseConnection();
			method.releaseConnection();
			CatchDataUtil.writeLog(fileName + " did not go on, next page " + (endCount+1) + " and seq " + endSeq + "\n");
		}
	}
}